package com.yunkaweilai.android.view.dialog.guadan;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.lazylibrary.util.StringUtils;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.interfaces.SimpleInterface;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.MyToast;

/**
 * 新增标签
 * Created by Administrator on 2016/12/22 0022.
 */

public class GuadanLableAddDialog extends Dialog {
    ImageView idImgClose;
    TextView idTvRight;
    EditText idEdtTypeName;
    private Context context;
    private SimpleInterface simpleInterface;

    public GuadanLableAddDialog(Context context, SimpleInterface simpleInterface) {
        super(context, R.style.dialog);
        this.context = context;
        this.simpleInterface = simpleInterface;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_lable_add);
        idImgClose = findViewById(R.id.id_img_close);
        idTvRight = findViewById(R.id.id_tv_right);
        idEdtTypeName = findViewById(R.id.id_edt_type_name);
        idImgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        idTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!StringUtils.isEmpty(idEdtTypeName.getText().toString())) {
                    addType();
                }
            }
        });
        Utils.autoShowKeyboard(idEdtTypeName);//弹出软键盘
    }

    private void addType() {
        HttpRequest.postUrl(Api.GUADAN_ADD_LABLE)
                .addParams("title", idEdtTypeName.getText().toString())
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(context, json);
                        if (checkCode) {
                            simpleInterface.nothing();
                            MyToast.showShortToast(context, "操作成功");
                            dismiss();
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        MyToast.showShortToast(context, "新增标签失败");
                    }
                });

    }


}
